package mysql_sync

import (
	"encoding/json"
	"fmt"
	"gc-rds-ha/internal/common"
	"gc-rds-ha/internal/dao/mysql_sync"
	"gc-rds-ha/internal/service/fetcher"

	log "github.com/sirupsen/logrus"
)

type RdsClusterOrch struct {
}

func (o *RdsClusterOrch) Forget(host string, port uint32) (*mysql_sync.DiscoverFormat, error) {
	forgetF := &mysql_sync.DiscoverFormat{}
	params := fetcher.FetchParams{
		Url:    fmt.Sprintf("%s%s/%s/%d", common.GetFetchUrl(), common.Forget, host, port),
		Method: "get",
	}
	forget, err := params.HttpRequest()
	if err != nil {
		return forgetF, err
	}
	if err = json.Unmarshal(forget, forgetF); err != nil {
		log.WithFields(log.Fields{
			"err": err,
		}).Error("orch Forget json.Unmarshal error")
		return forgetF, err
	}
	return forgetF, nil
}

func (o *RdsClusterOrch) Recover() {
}

func (o *RdsClusterOrch) ClusterAliasInfo(clusterAlias string) ([]mysql_sync.TopologyInfo, error) {
	var (
		topologyInfo []mysql_sync.TopologyInfo
		topologyUrl  = fmt.Sprintf("%s%s", common.GetFetchUrl(), common.ClusterAliasInfo)
	)
	params := &fetcher.FetchParams{
		Url:    fmt.Sprintf("%s/%s", topologyUrl, clusterAlias),
		Method: "get",
	}
	fetch, err := params.HttpRequest()
	if err != nil {
		return topologyInfo, err
	}
	if err = common.TTT.GetResponseForHttp(fetch, &topologyInfo); err != nil {
		return topologyInfo, err
	}
	return topologyInfo, nil
}

func (o *RdsClusterOrch) Discover(host string, port uint32) (*mysql_sync.DiscoverFormat, error) {
	discoverF := &mysql_sync.DiscoverFormat{}
	params := fetcher.FetchParams{
		Url:    fmt.Sprintf("%s%s/%s/%d", common.GetFetchUrl(), common.Discover, host, port),
		Method: "get",
	}
	discover, err := params.HttpRequest()
	if err != nil {
		return discoverF, err
	}
	if err = json.Unmarshal(discover, discoverF); err != nil {
		log.WithFields(log.Fields{
			"err": err,
		}).Error("orch Discover json.Unmarshal error")
		return discoverF, err
	}
	return discoverF, nil
}

func Topology(host string, port uint32) (*mysql_sync.TopologyFormat, error) {
	topologyF := &mysql_sync.TopologyFormat{}
	params := fetcher.FetchParams{
		Url:    fmt.Sprintf("%s%s/%s/%d", common.GetFetchUrl(), common.Topology, host, port),
		Method: "get",
	}
	topology, err := params.HttpRequest()
	if err != nil {
		return topologyF, err
	}
	if err = json.Unmarshal(topology, topologyF); err != nil {
		log.WithFields(log.Fields{
			"err": err,
		}).Error("isSlaveStop json.Unmarshal error")
		return topologyF, err
	}
	return topologyF, nil
}

func ClusterInfo() (*[]mysql_sync.ClusterInfo, error) {
	//先获取集群信息，再根据器群别名获取集群详情，入库
	var (
		clusterInfos *[]mysql_sync.ClusterInfo
		clusterUrl   = fmt.Sprintf("%s%s", common.GetFetchUrl(), common.ClusterInfo)
	)

	params := &fetcher.FetchParams{
		Url:    clusterUrl,
		Method: "get",
	}
	fetch, err := params.HttpRequest()
	if err != nil {
		return clusterInfos, err
	}
	if err = common.TTT.GetResponseForHttp(fetch, &clusterInfos); err != nil {
		return clusterInfos, err

	}
	return clusterInfos, nil
}
